#include "GUI/battlewindow.h"

#include <random>

#include "ui_battlewindow.h"

battleWindow::battleWindow(int level, int playerType, QWidget *parent)
    : QMainWindow(parent),
      enemyActionForecastWidget(new QWidget(this)),
      ui(new Ui::battleWindow) {
  ui->setupUi(this);
  this->setWindowTitle("战斗");
  this->setAttribute(Qt::WA_TranslucentBackground);
  this->level = level;  // 添加了一个全局变量level
  game = new WholeProgress(playerType, level);
  timer.stop();
  connect(game, &WholeProgress::showHurtNumber, this,
          &battleWindow::showNumber);  //

  this->showUgly(game->wholeEnemy.size());
  enemy_damage = game->wholeEnemy.begin()->getAtack();
  cardList = game->battleStart();
  this->hideButton();
  this->showCard();
  this->showHpMp();
  enemyAnimation = new QPropertyAnimation;
  enemyAnimation->setPropertyName("geometry");
  enemyAnimation->setDuration(700);

  playerAnimation = new QPropertyAnimation;
  playerAnimation->setPropertyName("geometry");
  playerAnimation->setDuration(700);

  count = new myTime();

  // switInEnd = 1;
  enter_store = false;
  enter_rest = false;
  ui->widget->hide();
  ui->widget_2->hide();
  ui->widget_3->hide();
  ui->widget_4->hide();
  type = playerType;

  if (type == 1) {
    ui->label_3->setPixmap(QPixmap(":/WarriorCards.png"));
  } else {
    ui->label_3->setPixmap(QPixmap(":/green.png"));
  }

  enemyActionForecastWidget->setGeometry(900, 100, 200,
                                         100);  // 你可以根据实际窗口尺寸调整
  enemyActionForecastWidget->setStyleSheet(
      "background-color: rgba(211, 211, 211, 0.8);"  // 浅灰色背景
      "border: 1px solid black;"                     // 边框颜色
      "border-radius: 15px;"                         // 圆角半径
  );


  QString enemy_action =
      QString::asprintf("Preparing to attack! Damage: %d", enemy_damage);
  enemyActionLabel = new QLabel(enemy_action, enemyActionForecastWidget);
  enemyActionLabel->setAlignment(Qt::AlignCenter);  // 居中对齐
  enemyActionLabel->setWordWrap(true);              // 启用自动换行
  enemyActionLabel->setStyleSheet(
      "color: black; font-size: 16px;");          // 黑色字体
  enemyActionLabel->setGeometry(0, 0, 200, 100);  // 设置为和预告框一样大
  enemyActionForecastWidget->show();
  enemyActionForecastWidget->raise();

  // 伤害数字格式设置
  for (int i = 0; i < 5; i++) {
    hurtNumber[i] = new QLabel("114514", this);
    hurtNumber[i]->setStyleSheet(
        "color: red; font-weight: bold; font-size: 35px;");
    hurtNumber[i]->setAlignment(Qt::AlignCenter);
    hurtNumber[i]->resize(250, 50);
    opacityEffect[i] = new QGraphicsOpacityEffect(hurtNumber[i]);
    hurtNumber[i]->setGraphicsEffect(opacityEffect[i]);
    numberMove[i] = new QPropertyAnimation(hurtNumber[i], "pos");
    numberMove[i]->setDuration(600);                  // 动画持续时间
    numberMove[i]->setStartValue(QPoint(1040, 200));  // 起始位置
    numberMove[i]->setEndValue(QPoint(1040, 120));    // 终点位置
    numberMove[i]->setEasingCurve(QEasingCurve::InOutQuad);
    numberOpacity[i] = new QPropertyAnimation(opacityEffect[i], "opacity");
    numberOpacity[i]->setDuration(600);    // 动画持续时间
    numberOpacity[i]->setStartValue(1.0);  // 起始不透明度
    numberOpacity[i]->setEndValue(0.0);
    animationGroup[i] = new QParallelAnimationGroup;
    animationGroup[i]->addAnimation(numberMove[i]);
    animationGroup[i]->addAnimation(numberOpacity[i]);
    hurtNumber[i]->hide();
  }

  // 休息时相关的初始化
  // 休息时的背景
  rest_back = new QLabel(this);
  rest_back->setPixmap(QPixmap(":/chunblack.jpg"));
  rest_back->setScaledContents(true);
  rest_back->resize(1478, 763);
  rest_back->move(-20, 0);
  rest_back->hide();
  // 休息的文字
  rest_text = new QLabel(this);
  rest_text->setPixmap(QPixmap(":/resting.png"));
  rest_text->setScaledContents(true);
  rest_text->resize(480, 366);
  int window_width = this->width();
  rest_text->move((window_width - rest_text->width()) / 2, -70);
  rest_text->hide();
  // 休息的动图
  rest_gif = new QLabel(this);
  gif = new QMovie(":/saber.gif");
  gif->start();
  rest_gif->setMovie(gif);
  rest_gif->setScaledContents(true);
  rest_gif->resize(600, 600);
  rest_gif->move((this->width() - rest_gif->width()) / 2, 200);
  rest_gif->hide();
  // 休息的计时器
  rest_timer = new QTimer(this);
  rest_timer->setInterval(3000);
  QObject::connect(rest_timer, &QTimer::timeout, [=]() {
    // 超时的处理,利用lambda函数实现
    qDebug() << "休息的计时结束";
    rest_back->hide();
    rest_text->hide();
    rest_gif->hide();
    rest_timer->stop();
  });
  // 事件文本自动换行
  ui->event_text->setWordWrap(true);

  event_num = 1;
}

battleWindow::~battleWindow() { delete ui; }

void battleWindow::usecard(int n) {
  // 使用卡牌
  qDebug() << "当前人物蓝量为： " << game->playername->getMp();
  qDebug() << "使用第" << n + 1 << "张牌";

  if (game->ifuse(n, type)) {
    // 卡牌音效
    // musicPlayer.playMusic(cardList[4].getMusicPath());
    this->action(n);  // 玩家动作
    card0 = game->dataReturn(n);

    cardList = game->returnCardInHand();

    if (cardList[0].getMp() == 1000) {
      vector<card>::iterator k = cardList.begin();
      cardList.erase(k);
    }

    // 玩家动作

    this->showHpMp();  // 更新血条蓝条

    this->hideButton();

    this->showCard();  // 显示当前牌；
    qDebug() << game->playername->manager->wastedCard.size()
             << "wastedCard.size";
    qDebug() << game->playername->manager->Card2DrugList.size()
             << "Card2DrugList.size";
    qDebug() << cardList.size();

    this->show_enemy_buff();   // 怪物buff
    this->show_player_buff();  // 玩家buff
    this->showEnemyAction();
  } else {
  }

  this->whoDeath();
}

void battleWindow::win() {
  // 游戏获胜
  qDebug() << "游戏获胜";
  endwindow *end = new endwindow();
  end->show();
  this->close();
}

// 战斗按钮
void battleWindow::fight_button(int n) {
  qDebug() << "fight" << n << "按下";
  this->showUgly(game->wholeEnemy.size());
  enemy_damage = game->wholeEnemy.begin()->getAtack();
  cardList = game->battleStart();

  this->hideButton();
  this->showCard();  // 显示当前牌；

  this->showHpMp();
  this->showEnemyAction();
  // switInEnd = 1;
  enter_store = false;
  enter_rest = false;

  ui->widget->hide();
}

// 休息按钮
void battleWindow::rest_button(int n) {
  qDebug() << "rest" << n << "按下";
  if (enter_rest == false) {
    if (this->level != 1) {
      // 最低难度允许重复休息
      enter_rest = true;
    }
    game->playername->setHp((11 - level));
    // 休息时黑屏3s
    rest_back->show();
    rest_text->show();
    random_gif();
    rest_gif->show();
    rest_timer->start();
    ui->label_6->setGeometry(
        60, 30,
        game->playername->getHp() * (460 / game->playername->getMaxHp()), 20);
  }
  // 刷新节点
  random_route();
  // musicPlayer.playMusic("qrc:/new/prefix1/Music/victory.ogg");
  hide_buff();
}

// 商店按钮
void battleWindow::store_button(int n) {
  qDebug() << "store" << n << "按下";
  if (enter_store == false) {
    enter_store = true;
    chooseCard = game->playername->manager->shopCard();
    ui->label_18->setText("你的金币：" +
                          QString::number(game->playername->getMoney()));
    ui->label_19->setText("花费:" + QString::number(chooseCard[0].getPrice()));
    ui->label_20->setText("花费:" + QString::number(chooseCard[1].getPrice()));
    ui->label_21->setText("花费:" + QString::number(chooseCard[2].getPrice()));
    ui->card1->setIcon(QIcon(chooseCard[0].getPicPath()));
    ui->card2->setIcon(QIcon(chooseCard[1].getPicPath()));
    ui->card3->setIcon(QIcon(chooseCard[2].getPicPath()));
    ui->card1->setVisible(true);
    ui->card2->setVisible(true);
    ui->card3->setVisible(true);
    ui->widget_2->show();
  }

  // 刷新节点
  random_route();
  // musicPlayer.playMusic("qrc:/new/prefix1/Music/victory.ogg");
  hide_buff();
}

void battleWindow::event_button(int n) {
  qDebug() << "event" << n << "按下";
  // 刷新节点
  random_route();
  event_num = random_event();
  ui->widget_4->show();
}

void battleWindow::on_pushButton_clicked() {
  // 打开选择窗口，重新开始游戏/
  chooseWindow *choose = new chooseWindow;

  choose->show();
  this->close();
}

void battleWindow::startbattle() {
  enemy_damage = game->wholeEnemy.begin()->getAtack();
  cardList = game->battleStart();

  // 这里你要展现卡牌然后重现window
}

void battleWindow::on_pushButton_2_clicked() { usecard(0); }

void battleWindow::on_pushButton_3_clicked() { usecard(1); }

void battleWindow::on_pushButton_4_clicked() { usecard(2); }

void battleWindow::on_pushButton_5_clicked() { usecard(3); }

void battleWindow::on_pushButton_6_clicked() { usecard(4); }

void battleWindow::on_pushButton_7_clicked() { usecard(5); }

void battleWindow::on_pushButton_8_clicked() { usecard(6); }

void battleWindow::on_pushButton_9_clicked() {
  // 按下回合结束
  game->myEnemy.setHp(0 - game->myEnemy.posionNum);
  game->playername->setHp(-100 * game->playername->getbuff(6));
  this->whoDeath();

  // 结束回合，怪物攻击

  game->playername->setMp(0 - game->playername->getMp());
  game->playername->setMp(game->playername->getMaxMp());
  n++;

  // 如果中毒层数不为0，则每汇合减1
  if (game->myEnemy.posionNum != 0) game->myEnemy.posionNum--;
  // 如果易伤层数不为0，则每回合减一
  if (game->myEnemy.getbuff(4)) game->myEnemy.addbuff(4, -1);
  // 怪物攻击
  enemyAnimation->setTargetObject(this->ui->label_4);
  enemyAnimation->setStartValue(QRect(1060, 200, 220, 160));
  enemyAnimation->setKeyValueAt(0.2, QRect(380, 200, 220, 160));
  enemyAnimation->setEndValue(QRect(1060, 220, 220, 160));
  enemyAnimation->start();

  // 计算实际攻击
  int tru_attack = game->myEnemy.getAtack();
  if (game->myEnemy.getbuff(3)) {
      tru_attack -= game->myEnemy.getbuff(3);
    game->myEnemy.addbuff(3, -1);
  }
  if (tru_attack < 0) {
    tru_attack = 0;
  }
  if (tru_attack - game->playername->getShield() > 0) {
    game->playername->setHp((0 - (tru_attack - game->playername->getShield())));
    emit showNumber(
        QString::number(-(tru_attack - game->playername->getShield())), 130,
        150, 0, QColorConstants::Red);
  } else {
    emit showNumber("格挡", 130, 150, 0, QColorConstants::Gray);
    game->playername->setShield(0 - tru_attack);
  }

  game->playername->setShield(0 - tru_attack);
  game->playername->calcuShield();  // 计算是否有需要保留到下回合的护盾

  this->showHpMp();  // 更新血条蓝条

  while (!cardList.empty()) {
    game->playername->manager->wastedCard.push_back(
        cardList[cardList.size() - 1]);
    cardList.pop_back();
  }
  qDebug() << game->playername->manager->wastedCard.size() << "wastedCard.size";
  qDebug() << game->playername->manager->Card2DrugList.size()
           << "Card2DrugList.size";
  qDebug() << cardList.size();
  cardList = game->updataCardList(game->playername->drawNumber);
  // qDebug()<<"grawcard"<<cardList.size();
  this->disableButton();
  this->hideButton();
  connect(&timer, &QTimer::timeout, this, &battleWindow::getCards);

  this->timer.start(100);

  this->showCard();  // 显示当前牌；

  this->whoDeath();
  this->showEnemyAction();
  this->show_enemy_buff();   // 怪物buff
  this->show_player_buff();  // 玩家buff
}

void battleWindow::on_fight1_clicked() { fight_button(1); }
void battleWindow::on_fight2_clicked() { fight_button(2); }
void battleWindow::on_fight3_clicked() { fight_button(3); }

void battleWindow::on_rest1_clicked() { rest_button(1); }
void battleWindow::on_rest2_clicked() { rest_button(2); }
void battleWindow::on_rest3_clicked() { rest_button(3); }

// 商店按钮
void battleWindow::on_store1_clicked() { store_button(1); }
void battleWindow::on_store2_clicked() { store_button(2); }
void battleWindow::on_store3_clicked() { store_button(3); }

void battleWindow::on_event1_clicked() { event_button(1); }
void battleWindow::on_event2_clicked() { event_button(2); }
void battleWindow::on_event3_clicked() { event_button(3); }

void battleWindow::hideButton() {
  ui->pushButton_2->setVisible(false);
  ui->pushButton_3->setVisible(false);
  ui->pushButton_4->setVisible(false);
  ui->pushButton_5->setVisible(false);
  ui->pushButton_6->setVisible(false);
  ui->pushButton_7->setVisible(false);
  ui->pushButton_8->setVisible(false);
}

void battleWindow::disableButton() {
  ui->pushButton_2->setEnabled(false);
  ui->pushButton_3->setEnabled(false);
  ui->pushButton_4->setEnabled(false);
  ui->pushButton_5->setEnabled(false);
  ui->pushButton_6->setEnabled(false);
  ui->pushButton_7->setEnabled(false);
  ui->pushButton_8->setEnabled(false);
}

void battleWindow::enableButton() {
  ui->pushButton_2->setEnabled(true);
  ui->pushButton_3->setEnabled(true);
  ui->pushButton_4->setEnabled(true);
  ui->pushButton_5->setEnabled(true);
  ui->pushButton_6->setEnabled(true);
  ui->pushButton_7->setEnabled(true);
  ui->pushButton_8->setEnabled(true);
}

void battleWindow::showCard() {
  for (size_t i = 0; i < cardList.size(); i++) {
    if (i == 0) {
      ui->pushButton_2->setIcon(QIcon(cardList[0].getPicPath()));
      ui->pushButton_2->setVisible(true);
    } else if (i == 1) {
      ui->pushButton_3->setIcon(QIcon(cardList[1].getPicPath()));
      ui->pushButton_3->setVisible(true);
    } else if (i == 2) {
      ui->pushButton_4->setIcon(QIcon(cardList[2].getPicPath()));
      ui->pushButton_4->setVisible(true);
    } else if (i == 3) {
      ui->pushButton_5->setIcon(QIcon(cardList[3].getPicPath()));
      ui->pushButton_5->setVisible(true);
    } else if (i == 4) {
      ui->pushButton_6->setIcon(QIcon(cardList[4].getPicPath()));
      ui->pushButton_6->setVisible(true);
    } else if (i == 5) {
      ui->pushButton_7->setIcon(QIcon(cardList[5].getPicPath()));
      ui->pushButton_7->setVisible(true);
    } else {
      ui->pushButton_8->setIcon(QIcon(cardList[6].getPicPath()));
      ui->pushButton_8->setVisible(true);
    }
  }
}

void battleWindow::showHpMp() {
  ui->label_6->setGeometry(
      60, 30, game->playername->getHp() * (460 / game->playername->getMaxHp()),
      20);  // 角色血条
  ui->label_7->setGeometry(70, 60, game->playername->getMp() * 144,
                           20);  // 角色蓝条
  ui->label_8->setGeometry(
      930 + (460 / game->myEnemy.getMaxHp()) *
                (game->myEnemy.getMaxHp() - game->myEnemy.getHp()),
      30, (460 / game->myEnemy.getMaxHp()) * game->myEnemy.getHp(),
      20);                                     //
                                               // 怪物血条
  ui->label_9->setGeometry(950, 60, 430, 20);  //
                                               // 怪物蓝条
  ui->label_10->setText(
      QString::number(game->playername->getHp()) + "/" +
      QString::number(game->playername->getMaxHp()));  //
                                                       // 角色血条数值
  ui->label_12->setText(QString::number(game->playername->getMp()) +
                        "/3");  //
                                // 角色蓝条数值
  ui->label_11->setText(
      QString::number(game->myEnemy.getHp()) + "/" +
      QString::number(game->myEnemy.getMaxHp()));  //
                                                   // 怪物血条数值
  ui->label_17->setText(QString::number(game->playername->getShield()));
}

// pa
void battleWindow::showEnemyAction() {
    // 敌方行动预告
    int tru_attack = game->myEnemy.getAtack();
    if (game->myEnemy.getbuff(3)) {
        tru_attack -= game->myEnemy.getbuff(3);
    }
    if (tru_attack < 0) {
        tru_attack = 0;
    }
  QString enemy_action =
      QString::asprintf("Preparing to attack! Damage: %d", tru_attack);
  enemyActionLabel->setText(enemy_action);
  enemyActionForecastWidget->show();
}

void battleWindow::on_card1_clicked() {
  qDebug() << "选择卡片1";
  if (game->playername->getMoney() >= chooseCard[0].getPrice()) {
    game->purchaseProgress(chooseCard[0].id);
    ui->label_18->setText("你的金币：" +
                          QString::number(game->playername->getMoney()));
    ui->card1->setVisible(false);
  }
}

void battleWindow::on_card2_clicked() {
  qDebug() << "选择卡片2";
  if (game->playername->getMoney() >= chooseCard[1].getPrice()) {
    game->purchaseProgress(chooseCard[1].id);
    ui->label_18->setText("你的金币：" +
                          QString::number(game->playername->getMoney()));
    ui->card2->setVisible(false);
  }
}

void battleWindow::on_card3_clicked() {
  qDebug() << "选择卡片3";
  if (game->playername->getMoney() >= chooseCard[2].getPrice()) {
    game->purchaseProgress(chooseCard[2].id);
      ui->label_18->setText("你的金币：" +
                            QString::number(game->playername->getMoney()));
      ui->card3->setVisible(false);
  }
}

void battleWindow::whoDeath() {
  if (game->playername->getHp() <= 0) {
    ui->widget_3->show();
    // musicPlayer.playMusic("qrc:/new/prefix1/Music/DeathMUSIC.ogg");
    hide_buff();

    if (type == 1)
      ui->label_3->setPixmap(QPixmap(":/reddeath.png"));
    else
      ui->label_3->setPixmap(QPixmap(":/greendeath.png"));
  } else if (game->myEnemy.getHp() <= 0) {
    game->playername->addMoney(1);
    enemyActionForecastWidget->hide();
    if (game->wholeEnemy.size() != 0) {
      // 还有怪未击败
      ui->widget->show();
      random_route();
      // musicPlayer.playMusic("qrc:/new/prefix1/Music/victory.ogg");
      hide_buff();
    } else {
      win();
    }
  }
}

void battleWindow::action(int num) {
  // 玩家动作
  // actType 1攻击，2防御，3回蓝
  if (cardList[num].actType == 1) {
    playerAnimation->setTargetObject(this->ui->label_3);
    playerAnimation->setStartValue(QRect(110, 180, 260, 190));
    playerAnimation->setKeyValueAt(0.2, QRect(800, 180, 260, 190));
    playerAnimation->setEndValue(QRect(110, 180, 260, 190));
    playerAnimation->start();
  } else if (cardList[num].actType == 2) {
    this->hideButton();

    this->showCard();
  } else {
    // musicPlayer.playMusic("qrc:/new/prefix1/Music/getBlue.ogg");
    ui->label_7->setGeometry(70, 60, game->playername->getMp() * 144, 20);
  }
}

void battleWindow::on_pushButton_10_clicked() {
  qDebug() << "pushButton 10 按下";
  ui->widget_2->hide();
}

void battleWindow::show_enemy_buff() {
  // 展示敌人的buff

  // 虚弱
  if (game->myEnemy.getbuff(3) > 0) {
    ui->label_24->setPixmap(QPixmap(":/weak.png"));
    ui->label_24->setVisible(true);
    qDebug() << "展示敌人虚弱";
  } else {
    ui->label_24->setVisible(false);
  }

  // 易伤
  if (game->myEnemy.getbuff(4) > 0) {
    ui->label_23->setPixmap(QPixmap(":/vulnerable.png"));
    ui->label_23->setVisible(true);
    qDebug() << "展示敌人易伤";
  } else {
    ui->label_23->setVisible(false);
  }
}

void battleWindow::hide_buff() {
  ui->label_23->hide();
  ui->label_24->hide();
  ui->label_27->hide();
  ui->label_28->hide();
  ui->label_29->hide();
  ui->label_30->hide();
  ui->label_31->hide();
  ui->label_32->hide();
}

void battleWindow::show_player_buff() {
  // 展示玩家的buff

  // 力量
  if (game->playername->getbuff(1) > 0) {
    ui->label_29->setPixmap(QPixmap(":/strength.png"));
    ui->label_29->setVisible(true);
    qDebug() << "展示玩家力量";
  } else {
    ui->label_29->setVisible(false);
  }

  // 敏捷
  if (game->playername->getbuff(2) > 0) {
    ui->label_30->setPixmap(QPixmap(":/dexterity.png"));
    ui->label_30->setVisible(true);
    qDebug() << "展示玩家敏捷";
  } else {
    ui->label_30->setVisible(false);
  }

  // 虚弱
  if (game->playername->getbuff(3) > 0) {
    ui->label_31->setPixmap(QPixmap(":/weak.png"));
    ui->label_31->setVisible(true);
    qDebug() << "展示玩家虚弱";
  } else {
    ui->label_31->setVisible(false);
  }

  // 易伤
  if (game->playername->getbuff(4) > 0) {
    ui->label_32->setPixmap(QPixmap(":/vulnerable.png"));
    ui->label_32->setVisible(true);
    qDebug() << "展示玩家易伤";
  } else {
    ui->label_32->setVisible(false);
  }

  // 暴走
  if (game->playername->getbuff(5) > 0) {
    ui->label_28->setPixmap(QPixmap(":/vigor.png"));
    ui->label_28->setVisible(true);
    qDebug() << "展示玩家暴走";
  } else {
    ui->label_28->setVisible(false);
  }

  // 死亡
  if (game->playername->getbuff(6) > 0) {
    ui->label_27->setPixmap(QPixmap(":/death.png"));
    ui->label_27->setVisible(true);
    qDebug() << "展示玩家死亡";
  } else {
    ui->label_27->setVisible(false);
  }
}

void battleWindow::showUgly(int num) {
  if (num == 1)
    ui->label_4->setPixmap(QPixmap(":/ugly10.png"));
  else if (num == 2)
    ui->label_4->setPixmap(QPixmap(":/ugly9.png"));
  else if (num == 3)
    ui->label_4->setPixmap(QPixmap(":/ugly8.png"));
  else if (num == 4)
    ui->label_4->setPixmap(QPixmap(":/ugly7.png"));
  else if (num == 5)
    ui->label_4->setPixmap(QPixmap(":/ugly6.png"));
  else if (num == 6)
    ui->label_4->setPixmap(QPixmap(":/ugly5.png"));
  else if (num == 7)
    ui->label_4->setPixmap(QPixmap(":/ugly4.png"));
  else if (num == 8)
    ui->label_4->setPixmap(QPixmap(":/ugly3.png"));
  else if (num == 9)
    ui->label_4->setPixmap(QPixmap(":/ugly2.png"));
  else {
    ui->label_4->setPixmap(QPixmap(":/ugly1.png"));
  }
}

// 复活
void battleWindow::on_comeback_clicked() {
  qDebug() << "复活";
  cardList = game->reStartBattle();
  // switInEnd = 1;
  enter_store = false;
  enter_rest = false;
  ui->widget_3->hide();
  showHpMp();
  showCard();
  if (type == 1)
    ui->label_3->setPixmap(QPixmap(":/WarriorCards.png"));
  else
    ui->label_3->setPixmap(QPixmap(":/green.png"));
}

void battleWindow::on_remake_clicked() {
  startWindow *start = new startWindow;

  start->show();
  this->close();
}
void battleWindow::getCards() {
  cardList.push_back(game->getACard());
  showCard();
  qDebug() << "getcard" << cardList.size();
  if (cardList.size() >= game->playername->drawNumber) {
    disconnect(&timer, &QTimer::timeout, this, &battleWindow::getCards);
    timer.stop();
    enableButton();
  }
}
void battleWindow::showNumber(QString a, int x, int y, int t, QColor c) {
  // qDebug() <<"slot get"<<a;
  // int x=a;
  QTimer::singleShot(t, [&, a, x, y, c]() {  // 延迟 1 秒
    // qDebug() <<"slot get"<<a<<x<<y;
    labelcnt++;
    labelcnt %= 5;
    hurtNumber[labelcnt]->setText(a);
    hurtNumber[labelcnt]->setStyleSheet(
        QString("font-weight: bold; font-size: 35px;color: %1;").arg(c.name()));
    numberMove[labelcnt]->setStartValue(QPoint(x, y));  // 起始位置
    numberMove[labelcnt]->setEndValue(QPoint(x, y - 80));
    animationGroup[labelcnt]->start();
    hurtNumber[labelcnt]->show();
  });
}

void battleWindow::random_route() {
  vector<int> random_digits;
  int min_value = 1;  // 包含
  int max_value = 5;  // 不包含
  for (int i = 0; i < 3; i++) {
    int random_num = QRandomGenerator::global()->bounded(min_value, max_value);
    if (enter_store && random_num == 3) {
      // 已经进入过商店,新的商店选项改为战斗
      random_num = 1;
    }
    if (enter_rest && random_num == 2) {
      // 已经休息过,新的休息选项改为战斗
      random_num = 1;
    }
    if (this->level == 1 && i == 0) {
      // 若选择新手难度,永远第一个为休息
      random_num = 2;
    }
    random_digits.push_back(random_num);
  }
  switch (random_digits[0]) {
    case 1:
      // 战斗
      ui->fight1->show();
      ui->rest1->hide();
      ui->store1->hide();
      ui->event1->hide();
      break;
    case 2:
      // 休息
      ui->fight1->hide();
      ui->rest1->show();
      ui->store1->hide();
      ui->event1->hide();
      break;
    case 3:
      // 商店
      ui->fight1->hide();
      ui->rest1->hide();
      ui->store1->show();
      ui->event1->hide();
      break;
    case 4:
      // 事件
      ui->fight1->hide();
      ui->rest1->hide();
      ui->store1->hide();
      ui->event1->show();
      break;
    default:
      break;
  }
  switch (random_digits[1]) {
    case 1:
      // 战斗
      ui->fight2->show();
      ui->rest2->hide();
      ui->store2->hide();
      ui->event2->hide();
      break;
    case 2:
      // 休息
      ui->fight2->hide();
      ui->rest2->show();
      ui->store2->hide();
      ui->event2->hide();
      break;
    case 3:
      // 商店
      ui->fight2->hide();
      ui->rest2->hide();
      ui->store2->show();
      ui->event2->hide();
      break;
    case 4:
      // 事件
      ui->fight2->hide();
      ui->rest2->hide();
      ui->store2->hide();
      ui->event2->show();
      break;
    default:
      break;
  }
  switch (random_digits[2]) {
    case 1:
      // 战斗
      ui->fight3->show();
      ui->rest3->hide();
      ui->store3->hide();
      ui->event3->hide();
      break;
    case 2:
      // 休息
      ui->fight3->hide();
      ui->rest3->show();
      ui->store3->hide();
      ui->event3->hide();
      break;
    case 3:
      // 商店
      ui->fight3->hide();
      ui->rest3->hide();
      ui->store3->show();
      ui->event3->hide();
      break;
    case 4:
      // 事件
      ui->fight3->hide();
      ui->rest3->hide();
      ui->store3->hide();
      ui->event3->show();
      break;
    default:
      break;
  }
}

void battleWindow::random_gif() {
  gif->stop();
  delete gif;
  int random_num = QRandomGenerator::global()->bounded(1, 4);
  switch (random_num) {
    case 1:
      gif = new QMovie(":/saber.gif");
      gif->start();
      break;
    case 2:
      gif = new QMovie(":/chon.gif");
      gif->start();
      break;
    case 3:
      gif = new QMovie(":/caster.gif");
      gif->start();
      break;
    case 4:
      gif = new QMovie(":/henshin.gif");
      gif->start();
      break;
    default:
      break;
  }
  rest_gif->setMovie(gif);
}
int battleWindow::random_event() {
  int random_num = QRandomGenerator::global()->bounded(1, 4);
  switch (random_num) {
    case 1:
      ui->event_pic->setStyleSheet("border-image:url(:/event1.jpg)3 10 3 10;");
      ui->event_text->setText(
          "你掉进了一个水坑里。可是坑里全是史莱姆黏液！你感觉到这黏液似乎会灼伤"
          "你，便拼命想要从坑中脱身。你的耳朵、鼻子和全身都被黏液给浸透了。爬出"
          "来后，你发现自己的金币似乎变少了。你回头一看，发现水坑里不但有你掉落"
          "的钱，还有不少其他不幸的冒险者们落下的金币。");
      ui->option_1->setText("收集金币: 获得7金币,失去5生命(不会死亡)。");
      ui->option_2->setText("放手吧: 失去5金币");
      break;
    case 2:
      ui->event_pic->setStyleSheet("border-image:url(:/event2.jpg)3 10 3 10;");
      ui->event_text->setText(
          "你发现附近的一处饮水池，里面流淌着源源不断闪着光的水");
      ui->option_1->setText("喝一口: 回复5生命");
      ui->option_2->setText("花钱许愿: 失去5金币,增加5最大生命值");
      break;
    case 3:
      ui->event_pic->setStyleSheet("border-image:url(:/event3.jpg)3 10 3 10;");
      ui->event_text->setText(
          "你想要从一群披着斗篷的人旁边偷偷潜行过去，这时一个皮肤发红的赤裸男人"
          "跑到了你的面前。“你能给我点儿什么吗,"
          "朋友？求求你了……一点小钱就好？”");
      ui->option_1->setText("施舍: 失去7金币");
      ui->option_2->setText("听着他的诅咒离开: 失去5最大生命值");
      break;
    default:
      break;
  }
  return random_num;
}

void battleWindow::on_option_1_clicked() {
  // to do 事件选项效果
  switch (event_num) {
    case 1:
      game->playername->addMoney(7);
      if (game->playername->getHp() > 5) {
        game->playername->setHp(-5);
      } else {
        // 先清空hp
        game->playername->setHp(-1000);
        game->playername->setHp(1);
      }
      break;
    case 2:
      game->playername->setHp(5);
      break;
    case 3:
      game->playername->addMoney(-7);
      break;
    default:
      break;
  }

  ui->widget_4->hide();
}

void battleWindow::on_option_2_clicked() {
  // to do 事件选项效果
  switch (event_num) {
    case 1:
      game->playername->addMoney(-5);
      break;
    case 2:
      game->playername->addMoney(-5);
      game->playername->addMaxHp(5);
      // 刷新hp
      game->playername->setHp(0);
      break;
    case 3:
      game->playername->addMaxHp(-5);
      // 刷新hp
      game->playername->setHp(0);
      break;
    default:
      break;
  }
  ui->widget_4->hide();
}
